package com.alili.springbooteditor.service;

import com.alili.springbooteditor.dao.EditorsDao;
import com.alili.springbooteditor.entities.editors;
import com.alili.springbooteditor.repository.EditorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EditorService {
    @Autowired
    EditorRepository editorRepository;
    @Autowired
    EditorsDao editorsDao;
    //找到前一篇文章，并且按照创建先后顺序排列分页
    public List<editors> listPageID(int pageNum){
        PageRequest pageRequest = PageRequest.of(pageNum, 5, Sort.Direction.DESC, "id");
        Page<editors> p = editorRepository.findAll(pageRequest);
        return p.getContent();
    }
    public editors getPreEditor(Integer id){
        for(int i=id-1;i>=0;i--) {
            editors preArt = editorsDao.getOneArticle(i);
            if(preArt!=null)
                return preArt;

        }
        return editorsDao.getOneArticle(id);
    }
    //找到后一篇文章，并且按照创建先后顺序排列分页
    public editors getNextEditor(Integer id){
        for(int k=id+1;k-id<50;k++) {
            editors nextArt = editorsDao.getOneArticle(k);
            if(nextArt!=null)
                return nextArt;
        }
        return editorsDao.getOneArticle(id);
    }
}

